package main.java.DMDE.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import main.java.DMDE.model.Method;

import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;

public abstract class CommonView {

    // Panel Attributes
    protected JPanel panel;
    protected JLabel nameText;
    protected JTextField name;
    protected JLabel typeText;
    protected JTextField type;
    protected JLabel visibCondition;
    protected JCheckBox finalCheckBox;
    protected JCheckBox staticCheckBox;
    protected JRadioButton btnPublic;
    protected JRadioButton btnPrivate;
    protected JRadioButton btnProtected;

    // List
    protected JButton addButton;
    protected JButton removeButton;
    protected JButton editButton;
    protected JScrollPane scrollPane;

    public CommonView() {

	initComponentsPanel();
	initList();
	initButtons();
    }

    protected void initComponentsPanel() {

	panel = new JPanel();
	nameText = new JLabel();
	name = new JTextField();
	typeText = new JLabel();
	type = new JTextField();
	visibCondition = new JLabel();
	visibCondition.setText("Visibility: ");
	finalCheckBox = new JCheckBox();
	staticCheckBox = new JCheckBox();
	btnPublic = new JRadioButton("Public");
	btnPrivate = new JRadioButton("Private");
	btnProtected = new JRadioButton("Protected");
	ButtonGroup group = new ButtonGroup();
	group.add(btnPrivate);
	group.add(btnProtected);
	group.add(btnPublic);
	nameText.setText("Name: ");
	nameText.setLabelFor(name);
	typeText.setText("Type: ");
	typeText.setLabelFor(type);
	finalCheckBox.setText("Final");
	staticCheckBox.setText("Static");

	addButton = new JButton();
	removeButton = new JButton();
	editButton = new JButton();
	scrollPane = new JScrollPane();

	panel.setLayout(new FormLayout(
		"4dlu,90dlu,8dlu,pref,4dlu,pref,4dlu,pref,4dlu,pref,4dlu",
		"4dlu,pref,10dlu,pref,10dlu,pref,10dlu,pref,5dlu,pref,10dlu,pref,5dlu,pref,fill:4dlu:grow"));

	panel.add(scrollPane, CC.xywh(2, 2, 1, 10));

	addButton.setText("Add");
	panel.add(addButton, CC.xy(4, 14));

	editButton.setText("Edit");
	panel.add(editButton, CC.xy(6, 14));

	removeButton.setText("Remove");
	panel.add(removeButton, CC.xy(8, 14));

	btnPublic.doClick();
	panel.add(typeText, CC.xy(4, 2));
	panel.add(type, CC.xyw(6, 2, 3));
	panel.add(nameText, CC.xy(4, 4));
	panel.add(name, CC.xyw(6, 4, 3));
	panel.add(staticCheckBox, CC.xy(4, 6));
	panel.add(finalCheckBox, CC.xy(6, 6));
	panel.add(visibCondition, CC.xy(4, 8));
	panel.add(btnPublic, CC.xy(4, 10));
	panel.add(btnPrivate, CC.xy(6, 10));
	panel.add(btnProtected, CC.xy(8, 10));

    }

    protected void initButtons() {

	addButton.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent actionEvent) {
		create();
	    }

	});

	editButton.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		edit();
	    }
	});

	removeButton.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		remove();
	    }
	});
	cleanView();
    }

    protected void cleanView() {
	name.setText("");
	type.setText("");
	btnPublic.doClick();
	if (finalCheckBox.isSelected())
	    finalCheckBox.doClick();
	if (staticCheckBox.isSelected())
	    staticCheckBox.doClick();
	cleanTree();
    }

    protected abstract void create();

    protected abstract void edit();

    protected abstract void remove();

    protected abstract void initList();

    protected abstract void cleanTree();

    
}
